#include "operationprocessor.h"

OperationProcessor::OperationProcessor()
{
}
OperationProcessor::~OperationProcessor()
{
}
//process add/delete
void OperationProcessor::process(Operation op,Record r,int option)
{
    this->operation=op;
    this->record=r;
    operationSucceeded=false;
    //process addRecord r
    if(operation==add_Record)
    {
        //add this Record to list
        //log the action
        superStack.addLog(operation,record,option);

        //change the flag
        operationSucceeded=true;
        if(option == 0){
            expenseRecordList.addRecord(record);
        }
        else
            incomeRecordList.addRecord(record);
    }
    if (operation==delete_Record)
    {
        //delete this Record from list
        //log the action
        superStack.addLog(operation,record,option);
        if(option == 0){
            expenseRecordList.deleteRecord(record);
        }
        else
            incomeRecordList.deleteRecord(record);
        //change the flag
        operationSucceeded=true;
    }
}
// process modify
void OperationProcessor::process(Operation op,Record oldRecord,Record newRecord,int option)
{
    this->operation=op;
    this->oldRecord=oldRecord;
    this->newRecord=newRecord;
    operationSucceeded=false;

    if(this->operation==modify_Record)
    {
        if(option == 0){
            expenseRecordList.modifyRecord(oldRecord, newRecord);
        }
        else
            incomeRecordList.modifyRecord(oldRecord,newRecord);

        superStack.addLog(this->operation,this->oldRecord,this->newRecord,option);

        operationSucceeded=true;
    }
}

//process undo-redo: undo/redo operation will not be logged into stack
void OperationProcessor::process(Operation op)
{
    this->operation=op;
    if(op==undo_Record)
    {
        if (superStack.can(undo_Record))
        {
            operationLog=superStack.getLog();
            if((operationLog.operationUndo()==add_Record)||(operationLog.operationUndo()==delete_Record))
                process(operationLog.operationUndo(),operationLog.getOldRecord(),operationLog.getOption());
            if(operationLog.operationUndo()==modify_Record)
                process(operationLog.operationUndo(),operationLog.getOldRecord(),operationLog.getNewRecord(),operationLog.getOption());
            operationSucceeded=true;
        }
        else
            operationSucceeded=false;
    }
}
ExpenseRecordList OperationProcessor::getExpenseList(){
    return this->expenseRecordList;
}
IncomeRecordList OperationProcessor::getIncomeList(){
    return this->incomeRecordList;
}
